home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_1231.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.7 KB  |  7 lines

  1. Different people have different opinions about coding standards.  However one thing we all should agree on is this: no style guide should impose undue performance penalties.  The real reason C++ allows objects to be created anywhere in the block is not style, but performance.
  2.  
  3. An object is initialized (constructed) the moment it is declared.  If you don't have enough information to initialize an object until half way down the fn, you can either initialize it to an 'empty' value at the top then 'assign' it later, or initialize it correctly half way down the fn.  It doesn't take much imagination to see that it's cheaper to get it right the first time than it is to build it once, tear it down, then rebuild it again.  Simple examples show a factor of 350% speed hit for simple classes like String.  Your mileage may vary; surely the overall system degradation will be less that 300+%, but there *will* be degradation.  *Unnecessary* degradation.
  4.  
  5. A common retort to the above is: 'we'll provide "set" methods for every datum in our objects, so the cost of construction will be spread out'.  This is worse than the performance overhead, since now you're introducing a maintenance nightmare.  Providing 'set' methods for every datum is tantamount to public data.  You've exposed your implementation technique to the world.  The only thing you've hidden is the physical *names* of your subobjects, but the fact that you're using a List and a String and a float (for example) is open for all to see.  Maintenance generally consumes far more resources than run-time CPU.
  6.  
  7. Conclusion: in general, locals should be declared near their first use.  Sorry that this isn't 'familiar' to your C experts, but 'new' doesn't necessarily mean 'bad'.